package plus.exp;

import plus.B;
import plus.Gamma;
import plus.S_Array;
import plus.run.Emit;

public class E_Index extends Exp {

	final Exp array;
	final Exp index;

	public E_Index(Exp $1, Exp $3) { array = $1; index = $3; }
	
	@Override
	public void threadScope() {
		array.threadScope();
		array.type.conform_to(B._array, new Gamma());
		index.threadScope();
		index.unify_with(B._int);
		S_Array sol = (S_Array)(array.type.getSolution());
		assert sol != null;
		type = sol.range;
	}
	@Override String impliedName() { return array.impliedName(); }
	@Override String aThis() { return "an array element"; }
	@Override
	public void threadAssign() { threadScope(); }
	@Override
	public void compileAssign(Exp rhs) {
		array.compilePush();
		rhs.compilePush();
		index.compilePush();
		Emit.storeIndex();
		Emit.pop();
	}
	@Override
	public void compilePush() {
		array.compilePush();
		index.compilePush();
		Emit.loadIndex();
	}
}
